---------------------------------------------------------------------------------------------------- -- Таймер с выводом изображения на экран. ---------------------------------------------------------------------------------------------------- local k,v = 0,0 class "action_timer" function action_timer:__init(obj, storage) self.object = obj self.st = storage end function action_timer:update(delta) local actor = db.actor if xr_logic.try_switch_to_another_section(self.object, self.st, actor) then return end -- Высчитываем сколько времени уже работает счетчик local nn = time_global() - db.storage[self.object:id()].activation_time -- Изменяем значение счетчика local value_time = 0 if self.st.type == "inc" then value_time = self.st.start_value + nn else value_time = self.st.start_value - nn end if value_time <= 0 then value_time = 0 end -- Формируем строку счетчика local hours = math.floor(value_time/3600000) local minutes = math.floor(value_time/60000 - hours*60) local seconds = math.floor(value_time/1000 - hours*3600 - minutes*60) local str = tostring(hours)..":"..sr_timer.time2str(minutes)..":"..sr_timer.time2str(seconds) self.st.timer:TextControl():SetTextST(str) -- Определяем нужно ли куда то переходить. for k,v in pairs(self.st.on_value) do if (self.st.type == "dec" and value_time <= v.dist) or (self.st.type == "inc" and value_time >= v.dist) then -- Обработка значения. xr_logic.switch_to_section(self.object, self.st.ini, xr_logic.pick_section_from_condlist(db.actor, self.object, v.state)) end end end function action_timer:deactivate(delta) local hud = get_hud() if (hud) then hud:RemoveCustomStatic(self.st.timer_id) if self.st.string ~= nil then hud:RemoveCustomStatic("hud_timer_text") end end end --function action_timer:save() -- save_var( self.object, "timer_value", self.state ) --end function time2str(n) if n >= 10 then return tostring(n) else return "0"..tostring(n) end end --------------------------------------------------------------------------------------------------------------------- function add_to_binder(obj, ini, scheme, section, storage) local action = action_timer(obj, storage) xr_logic.subscribe_action_for_events(obj, storage, action) end function set_scheme(obj, ini, scheme, section, gulag_name) local st = xr_logic.assign_storage_and_bind(obj, ini, scheme, section) st.logic = xr_logic.cfg_get_switch_conditions(ini, section, obj) st.type = ini:r_string_ex(section,"type") or "inc" if st.type ~= "inc" and st.type ~= "dec" then abort("ERROR: wrong sr_timer type. Section [%s], Restrictor [%s]", section, obj:name()) end st.start_value = ini:r_float_ex(section,"start_value") or 0 -- Вычитываем значения перехода. st.on_value = parse_data(obj, ini:r_string_ex(section,"on_value")) st.timer_id = ini:r_string_ex(section,"timer_id") or "hud_timer" st.string = ini:r_string_ex(section,"string") local hud = get_hud() hud:AddCustomStatic(st.timer_id, true) st.timer = hud:GetCustomStatic(st.timer_id):wnd() if st.string ~= nil then hud:AddCustomStatic("hud_timer_text", true) local timer_text = hud:GetCustomStatic("hud_timer_text"):wnd() timer_text:TextControl():SetTextST(st.string) end end function parse_data(npc,s) local t = {} if s then local size_t = 0 for name in string.gmatch( s, "(%|*%d+%|[^%|]+)%p*" ) do local dat = {dist = nil, state = nil} local t_pos = string.find( name, "|", 1, true ) local dist = string.sub( name, 1, t_pos - 1 ) local state = string.sub( name, t_pos + 1) dat.dist = tonumber(dist) if state then dat.state = xr_logic.parse_condlist(npc, dist, state, state) end size_t = size_t + 1 t[size_t] = dat end end return t end